package com.zhangwei.xmppdemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.mobsandgeeks.saripaar.ValidationError;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.Length;
import com.zhangwei.xmppdemo.xmpp.XmppHelper;
import com.zhangwei.xmppdemo.xmpp.api.XmppCallBack;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.List;

/**
 * 验证框架
 * 在需要验证的控件上声明注解
 * 第一步实例化Validator对象
 * 第二步 添加ValidationListener接口
 * 第三步 根据业务需求 实现成功与失败回调接口的方法
 */
@EActivity(R.layout.activity_login)
public class LoginActivity extends AppCompatActivity implements Validator.ValidationListener {
    @ViewById
    Toolbar toolbar;
    @ViewById
    ImageView loginTopImv;
    @ViewById
    @Length(min = 6, max = 18, trim = true, message = "最小长度不能小于6,最大不能超过18")
    EditText idLoginUsernameEdt;
    @ViewById
    @Length(min = 6, max = 18, trim = true, message = "最小长度不能小于6,最大不能超过18")
    EditText loginPasswordEdt;
    @ViewById
    Button loginLoginBtn;
    @ViewById
    Button loginRegisterBtn;
    @ViewById
    TextView loginForgotTv;
    private Validator validator;


    @AfterViews
    public void init() {
        validator = new Validator(this);
        validator.setValidationListener(this);
    }

    @Click({R.id.login_login_btn, R.id.login_register_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_login_btn:
                validator.validate();
                break;
            case R.id.login_register_btn:
                startActivity(new Intent(this, RegisterActivity_.class));
                this.finish();
                break;
        }
    }

    /**
     * 验证成功回调
     */
    @Override
    public void onValidationSucceeded() {
        String name = idLoginUsernameEdt.getText().toString().trim();
        String password = loginPasswordEdt.getText().toString().trim();
        XmppHelper.getInstance().login(name, password, new XmppCallBack<Integer>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onSuccess(Integer integer) {
                switch (integer) {
                    case -1:
                        //登陆失败
                        break;
                    case 1:
                        //登陆成功跳转
                        startActivity(new Intent(LoginActivity.this, MainActivity_.class));
                        break;
                    case 2:
                        //连接服务器失败
                        break;
                }
            }
        });
    }

    /**
     * 验证失败回调
     *
     * @param errors
     */
    @Override
    public void onValidationFailed(List<ValidationError> errors) {
        if (errors != null && errors.size() > 0) {
            for (int i = 0; i < errors.size(); i++) {
                ValidationError validationError = errors.get(i);
                String errorMessage = validationError.getCollatedErrorMessage(this);
                if (validationError.getView() instanceof EditText) {
                    EditText editText = (EditText) validationError.getView();
                    editText.setError(errorMessage);
                    editText.requestFocus();
                } else {
                    Toast.makeText(LoginActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
                }

            }

        }


    }
}
